package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class HljtvNewsAnalyse implements AnalyseNews{
	
	private static final String[] regex={
				"http://www.topenergy.org/news_[0-9]*.html",
				"http://[a-z]*.hljtv.com/[0-9]*/[0-9]*/[0-9]*.shtml"};
			
			public boolean isDetailPage(String url) {
				for (int i=0; i < regex.length; i++) {
					if (url.matches(regex[i])) {
						return true;
					}
				}
				return false;
			}
			
			
			public NewsMeta Update(NewsMeta meta) {
				return null;
			}

			

			
			public NewsMeta parserHtml(UrlMeta urlMeta) {
				NewsMeta news=new NewsMeta();
				if (urlMeta.getHtml() == null) {
				}		
				String htmltxt=urlMeta.getHtml();
				String url=urlMeta.getUrl();		
				if(!isDetailPage(url)){
				}				
				
				
				String title=null;
				String content=null;
				Long date=null;
				Document doc=Jsoup.parse(htmltxt);
				title=doc.select("h1").text();
		
				if(StringUtils.isBlank(title)){
					title=doc.select("h1 [style=width:630px;]").text();
					if(StringUtils.isBlank(title)){
						title=doc.select("div.gq_left span").text();
						if(StringUtils.isBlank(title)){
							title=doc.select("h1#artibodyTitle").text();
						}
					}
				}
				content=HtmlCleaner.getContentHtml(url,doc.select("div.articlebody"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("td#text"));
					if(StringUtils.isBlank(content)){
						content=HtmlCleaner.getContentHtml(url,doc.select(".pic-title-summary"));
						if(StringUtils.isBlank(content)){
							content=HtmlCleaner.getContentHtml(url,doc.select(".article_cbody"));
							if(StringUtils.isBlank(content)){
								content=HtmlCleaner.getContentHtml(url,doc.select("li.con_up_infos"));
							}
						}
					}
				}
				date=DateUtils.matchDate(doc.select("div.artInfo").text());
				if(date == null){
					date=DateUtils.matchDate(doc.select("tr[style=HEIGHT: 28px]").text());
					if(date == null){
						date=DateUtils.matchDate(doc.select(".gall-title span").text());
						if(date == null){
							date=DateUtils.matchDate(doc.select("h3").text());
							if(date == null){
								date=DateUtils.matchDate(doc.select("div#con_content div.con_videoinfo").text());
								if(date == null){
								 date=DateUtils.matchDate(url);
								}
							}
						}
					}
				}
				/**
				 * 初始化、截取 、设置作者信息*******************************************
				 * 
				 * */
				String str=null;
				String author=null;
				str=doc.select(".gall-title span").text();
				if(StringUtils.isBlank(str))
					str=doc.select("h3").text();
				author=StringUtils.substringBetween(str, "来源："," ");
				if(StringUtils.isBlank(author)){
					str=doc.select(".artInfo").text();
					author=StringUtils.substringBetween(str, "来源："," ");
					if(StringUtils.isBlank(author))
						author=StringUtils.substringBetween(str, "来源：","作者");
					if(StringUtils.isBlank(author))
						author=StringUtils.substringBetween(str, "来源：","我要");
				}
				news.setAuthor(author);
				/**
				 *                        	 **************************end *************************
				 * 
				 **/
				news.setUrl(url);		
				news.setTitle(title);
				news.setContent(content);
				news.setDate(date);
				return news;
			}
			
			public static void main(String[] args) {
				HljtvNewsAnalyse test=new HljtvNewsAnalyse();
				String url="http://news.hljtv.com/2014/0321/550508.shtml";
				System.out.println(test.isDetailPage(url));
				UrlMeta meta=CrawlHTML.responseToURL(url);
				NewsMeta parserHtml=test.parserHtml(meta);
				System.out.println(parserHtml);
			}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
